package com.vm.activity;

import java.util.List;

import com.vm.db.UpgradeSoftDao;
import com.vm.model.UpgradeSoft;
import com.vm.myview.ViewUpgradeUtils;
import com.wwtscapp.apk.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ManagerActivity extends Activity {
	List<UpgradeSoft> appList;
	RelativeLayout rlayout_upgrade;
	Button btn_software_upgrade_number;
	Button btn_software_uninstall;
	Button btn_installation_package_management;
	// Button btn_download_management;
	Button btn_clean_up;
	Button btn_share;
	Button btn_skin;
	Button btn_feedback;
	Button btn_set_up;
	Button btn_exit;
	Button btn_accelerate;
	Button btn_about;
	ImageView imgbtn_search_back;
	ViewUpgradeUtils viewUpgradeUtils;
	UpgradeSoftDao dao;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manage1);
		initButton();
		viewUpgradeUtils = new ViewUpgradeUtils(this, this);
		dao = new UpgradeSoftDao();
		appList = dao.getUpgradeSofts();
		btn_software_upgrade_number.setText(Integer.toString(appList.size()));
	}

	public void initButton() {
		imgbtn_search_back = (ImageView) findViewById(R.id.imgbtn_search_back);
		imgbtn_search_back.setOnClickListener(btn_backListener);
		rlayout_upgrade = (RelativeLayout) findViewById(R.id.rlayout_upgrade);
		btn_software_upgrade_number = (Button) findViewById(R.id.btn_software_upgrade_number);
		btn_software_uninstall = (Button) findViewById(R.id.btn_software_uninstall);
		btn_installation_package_management = (Button) findViewById(R.id.btn_installation_package_management);
		// btn_download_management =
		// (Button)findViewById(R.id.btn_download_management);
		btn_clean_up = (Button) findViewById(R.id.btn_clean_up);
		btn_accelerate = (Button) findViewById(R.id.btn_accelerate);
		btn_share = (Button) findViewById(R.id.btn_share);
		btn_skin = (Button) findViewById(R.id.btn_skin);
		btn_feedback = (Button) findViewById(R.id.btn_feedback);
		btn_set_up = (Button) findViewById(R.id.btn_set_up);
		btn_about = (Button) findViewById(R.id.btn_about);
		rlayout_upgrade.setOnClickListener(btnClickListener);
		btn_software_uninstall.setOnClickListener(btnClickListener);
		btn_installation_package_management
				.setOnClickListener(btnClickListener);
		btn_clean_up.setOnClickListener(btnClickListener);
		btn_accelerate.setOnClickListener(btnClickListener);
		btn_share.setOnClickListener(btnClickListener);
		btn_skin.setOnClickListener(btnClickListener);
		btn_feedback.setOnClickListener(btnClickListener);
		btn_set_up.setOnClickListener(btnClickListener);
		btn_about.setOnClickListener(btnClickListener);

	}

	public OnClickListener btnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent;
			switch (v.getId()) {
			case R.id.rlayout_upgrade:
				intent = new Intent(ManagerActivity.this,
						SoftUpgradeActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_software_uninstall:
				intent = new Intent(ManagerActivity.this,
						SoftUninstallActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_installation_package_management:
				intent = new Intent(ManagerActivity.this,
						PackageManagerActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_clean_up:
				final MangerDialog dialog1 = new MangerDialog(
						ManagerActivity.this);
				dialog1.show();
				dialog1.setClicklistener(new MangerDialog.ClickListenerInterface() {

					@Override
					public void doConfirm() {
						// TODO Auto-generated method stub
						dialog1.dismiss();
						startAnotherApp("com.tencent.qqpimsecure");
					}

					@Override
					public void doCancel() {
						// TODO Auto-generated method stub
						dialog1.dismiss();
					}
				});
				break;
			case R.id.btn_accelerate:
				final MangerDialog dialog2 = new MangerDialog(
						ManagerActivity.this);
				dialog2.show();
				dialog2.setClicklistener(new MangerDialog.ClickListenerInterface() {

					@Override
					public void doConfirm() {
						// TODO Auto-generated method stub
						dialog2.dismiss();
						startAnotherApp("com.tencent.qqpimsecure");
					}

					@Override
					public void doCancel() {
						// TODO Auto-generated method stub
						dialog2.dismiss();
					}
				});
				break;
			case R.id.btn_share:
				intent = new Intent(ManagerActivity.this, ShareActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_skin:
				intent = new Intent(ManagerActivity.this,
						DownloadManageActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_feedback:
				intent = new Intent(ManagerActivity.this,
						FeedBackActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_about:
				intent = new Intent(ManagerActivity.this, AboutAcitivity.class);
				startActivity(intent);
				break;

			case R.id.btn_set_up:
				intent = new Intent(ManagerActivity.this, SettingActivity.class);
				startActivity(intent);
				break;
			case R.id.btn_exit:

				break;

			default:
				break;
			}

		}
	};

	private void startAnotherApp(String packageName) {
		// PackageInfo packageInfo = null;
		try {
			Intent LaunchIntent = ManagerActivity.this.getPackageManager()
					.getLaunchIntentForPackage(packageName);
			startActivity(LaunchIntent);
			// packageInfo = getPackageManager().getPackageInfo(packageName, 0);
			// if (packageInfo == null) {
			//
			// } else {
			// System.out.println("packageInfo!=null");
			// Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
			// resolveIntent.setData(Uri
			// .parse("app://jp.co.cybird.barcodefootballer/"));
			// resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
			// resolveIntent.setPackage(packageInfo.packageName);
			// System.out
			// .println("packageInfo.packageName=" + packageInfo.packageName);
			//
			// List<ResolveInfo> resolveInfoList = getPackageManager()
			// .queryIntentActivities(resolveIntent, 0);
			//
			// System.out.println("resolveInfoList.size()=" +
			// resolveInfoList.size());
			//
			// ResolveInfo resolveInfo = resolveInfoList.iterator().next();
			// if (resolveInfo != null) {
			// String activityPackageName =
			// resolveInfo.activityInfo.packageName;
			// String className = resolveInfo.activityInfo.name;
			//
			// Intent intent = new Intent(Intent.ACTION_MAIN);
			// intent.addCategory(Intent.CATEGORY_LAUNCHER);
			// ComponentName componentName = new ComponentName(
			// activityPackageName, className);
			//
			// intent.setComponent(componentName);
			// startActivity(intent);
			// }
			//
			// }
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("packageInfo==null");
			Intent intent = new Intent(ManagerActivity.this,
					DetailActivity.class);
			intent.putExtra("softId", "579d884a-4011-4d5f-a6ed-f4f2580533bb");
			startActivity(intent);
		}

		// <data android:scheme="app"
		// android:host="jp.co.cybird.barcodefootballer/" />

	}

	/**
	 * imgbtn_search_back 返回按钮监听
	 */
	OnClickListener btn_backListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			onBackPressed();
		}
	};

	/**
	 * 手机自带返回按钮监听
	 */
	@Override
	public void onBackPressed() {
		super.onBackPressed();
	}
}